﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Power.Model;
using System.Data;
using System.Data.SqlClient;

namespace Power.DAL
{
    public class CustomerDAL
    {
        public bool JudgeByDevice(string devicet)
        {
            DataTable table = SqlHelper.ExecuteDataTable("select * from T_Customer where Device=@device and IsDeleted=0",
                new SqlParameter("@device", devicet));
            if (table.Rows.Count == 0)
            {
                return false;
            }

            else
            {
                return true;
            }
        }




        public void InsertnUser(Customer op)
        {
            //bit类型，在sql语句中要写0、1
            //在.net中要用bool表示
            SqlHelper.ExecuteNonQuery(@"insert into T_Customer(
                CusName,Address,Device,Charger,Phone,CID,IsDeleted) values(@cusName,@address,@device,@charger,@phone,@cid,0)",
                    new SqlParameter("@cusName", op.CusName),
                    new SqlParameter("@address", op.Address),
                    new SqlParameter("@device", op.Device),
                    new SqlParameter("@charger", op.Charger),
                    new SqlParameter("@phone", op.Phone),
                    new SqlParameter("@cid", op.CID));

            SqlHelper.ExecuteNonQuery(@"insert into T_DeviceIP(
                Device,IP) values(@device,'127.0.0.1')",

        new SqlParameter("@device", op.Device));




        }



        public void CusDel(string cusName)
        {
            SqlHelper.ExecuteNonQuery("update T_Customer set IsDeleted=1 where CusName=@cusname",
               new SqlParameter("@cusname", cusName));
        }


        public void CusMon(string devName)
        {
            SqlHelper.ExecuteNonQuery("update T_Customer set IsMon=1 where Device=@device",
               new SqlParameter("@device", devName));
        }

        public void CusDeMon(string devName)
        {
            SqlHelper.ExecuteNonQuery("update T_Customer set IsMon=0 where Device=@device",
               new SqlParameter("@device", devName));
        }



        public Customer[] ListToRP()
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Customer where IsDeleted=0 and IsMon=1");
            Customer[] Customers = new Customer[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Customers[i] = ToCustomer(dt.Rows[i]);
            }
            return Customers;
        }




        public Customer[] ListToFind(string sttext)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Customer where IsDeleted=0 and( CusName= @Temppar or Phone=@Temppar)", new SqlParameter("@Temppar", sttext));
            Customer[] Customers = new Customer[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Customers[i] = ToCustomer(dt.Rows[i]);
            }
            return Customers;
        }


        public Customer DeToFind(string sttext)
        {
            DataTable dt = SqlHelper.ExecuteDataTable("select * from T_Customer where IsDeleted=0 and Device= @Temppar", new SqlParameter("@Temppar", sttext));
            Customer Customers = new Customer();

                Customers = ToCustomer(dt.Rows[0]);

            return Customers;
        }






        public void CusUpdate(long id,string cusname, string address, string device,string CID,string phone)
        {
            SqlHelper.ExecuteNonQuery("update T_Customer set CusName=@cusname,Address=@address,Device=@device,CID=@CID,Phone=@phone where Id=@id",
               new SqlParameter("@id", id),
               new SqlParameter("@cusname", cusname),
               new SqlParameter("@address", address),
               new SqlParameter("@device", device),
               new SqlParameter("@CID", CID),
               new SqlParameter("@phone", phone)
               
               );
        }



        private Customer ToCustomer(DataRow row)
        {
            Customer op = new Customer();
            op.Id = (long)row["Id"];
            op.CusName = (string)row["CusName"];
            op.Charger = (string)row["Charger"];
            op.Address = (string)row["Address"];
            op.Device = (string)row["Device"];
            op.IsDeleted = (bool)row["IsDeleted"];
            op.Phone = (string)row["Phone"];
            op.CID = (string)row["CID"];
            return op;
        }






















    }
}
